//
//  ZPlayerController.swift
//  ZPlayer
//
//  Created by zuber on 2018/11/15.
//  Copyright © 2018年 zuber. All rights reserved.
//

import UIKit

class ZPlayerController: UIViewController {
  
  private var playerView: ZPlayer!
  
  let urlString: String
  init(_ urlString: String) {
    self.urlString = urlString
    super.init(nibName: nil, bundle: nil)
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.black
    playerView = ZPlayer()
    if let url = URL(string: self.urlString) {
      view.addSubview(playerView)
      playerView.snp.makeConstraints { (make) in
        make.edges.equalToSuperview()
      }
      playerView.backgroundColor = UIColor.black
      playerView.delegate = self
      playerView.owner = self
      playerView.type = .url(url)
    } 
  }
  
  deinit {
    self.playerView.releaseResource()
    print("ZPlayerController deinit")
  }
  
}


// MARK: - ZPlayerDelegate

extension ZPlayerController: ZPlayerDelegate {

  func zplayerDidClose(_ view: ZPlayer) {
    self.dismiss(animated: true, completion: nil)
  }
  
}
